<?php
namespace app\appclass\controller;
use app\AdminRest;

use app\ApiRest;
use app\appclass\model\CardList;
use app\appclass\model\CardUser;
use app\appclass\model\CardWater;
use app\appclass\model\ClassCard;
use app\appclass\model\ClassCate;
use app\appclass\model\ClassCoach;
use app\appclass\model\ClassDate;
use app\appclass\model\ClassLable;
use app\appclass\model\ClassList;
use app\appclass\model\ClassRecord;
use app\appclass\model\ClassRoom;
use app\appclass\model\Lable;
use app\appclass\model\StoreInfo;
use app\card\model\Company;
use app\shop\model\IndexUserInfo;
use think\App;
use think\facade\Db;


class IndexList extends ApiRest
{

    protected $model;

    protected $record_model;

    protected $lable_model;

    protected $room_model;

    protected $date_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model       = new ClassList();

        $this->record_model  = new ClassRecord();

        $this->lable_model = new ClassLable();

        $this->room_model  = new ClassRoom();

        $this->date_model  = new ClassDate();

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 17:50
     * @功能说明:课程列表
     */

    public function classList(){

        $this->date_model->cancelfailClass($this->_uniacid);

        $input = $this->_param;

        $store_id = $this->model->getStoreId($input['staff_id']);

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.store_id','=',$store_id];

        $dis[] = ['a.status','>',0];

        if(!empty($input['cate_id'])){

            $dis[] = ['a.cate_id','=',$input['cate_id']];

        }

        if(!empty($input['start_time'])){

            $start_time = strtotime(date('Y-m-d',$input['start_time']));

            $end_time   = $start_time+86399;

            $dis[] = ['a.start_time','between',"$start_time,$end_time"];

        }

        $data = $this->date_model->indexDataList($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-14 11:07
     * @功能说明:分类
     */
    public function cateList(){

        $input = $this->_param;

        $store_id = $this->model->getStoreId($input['staff_id']);

        $dis = [

            'status'  => 1,

            'store_id'=> $store_id
        ];

        $cate_model = new ClassCate();

        $data = $cate_model->where($dis)->order('id desc')->select()->toArray();

        return $this->success($data);


    }

    /**
     * @author chenniang
     * @DataTime: 2021-05-13 13:39
     * @功能说明:排课详情
     */
    public function classDateInfo(){

        $input = $this->_param;

        $company_model = new Company();

        $class_card_model = new ClassCard();

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->date_model->dataInfo($dis);

        $class_lable = new ClassLable();

        $room_model  = new ClassRoom();
        //教练名字
        $data['coach_name']   = lbUserName($data['coach_id']);
        //教练头像
        $data['coach_avatar'] = lbUserAvatar($data['coach_id']);
        //标签
        $data['lable'] = $class_lable->classLable($data['class_id']);
        //门店名字
        $data['store_name']  = $company_model->where(['id'=>$data['store_id']])->value('name');
        //可用会员卡
        $data['canuse_card'] = $class_card_model->canUseCard($data,$this->getUserId());
        //教室
        $data['room_name']   = $room_model->where(['id'=>$data['room_id']])->value('title');
        //时间
        $data['hour_text'] = date('H:i',$data['start_time']).'-'.date('H:i',$data['end_time']);

        $data['day_text']  = date('Y-m-d',$data['start_time']);

        $data['over_num']  = $data['num'] - $data['have_num'];

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-17 16:41
     * @功能说明:预约
     */
    public function appClassDate(){

        $input = $this->_input;

        $res   = $this->record_model->appClassDate($input,$this->_uniacid,$this->getUserId());

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-18 10:37
     * @功能说明:教师排课列表
     */
    public function coachClassList(){

        $input = $this->_param;

        $cate_model = new ClassCate();

        $class_coach_model  = new ClassCoach();

        $cate_type = $cate_model->where(['id'=>$input['cate_id']])->value('type');

        if($cate_type==1){

            $data = $class_coach_model->classCoachList(['a.coach_id'=>$input['coach_id'],'c.type'=>1,'b.status'=>1]);

        }else{

            $this->date_model->cancelfailClass($this->_uniacid);

            $dis = [

                'coach_id'=> $input['coach_id'],

                'cate_id' => $input['cate_id'],

                'status'  => 1
            ];

            $data = $this->date_model->indexDataListV2($dis);

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-18 13:45
     * @功能说明:私教课详情
     */
    public function privateClassInfo(){

        $input = $this->_param;

        $class_coach_model  = new ClassCoach();
        //教练名字
        $data['coach_name'] = lbUserName($input['coach_id']);
        //私交课程名字
        $data['class_list'] = $class_coach_model->classCoach(['a.coach_id'=>$input['coach_id'],'c.type'=>1,'b.status'=>1]);

        $data['time_list'] = $this->record_model->getCoachTime($input['coach_id'],$input['time']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-13 10:22
     * @功能说明:私交课教练列表
     */
    public function privateCoachList(){

        $input = $this->_param;

        $card_model = new CardList();

        $input['staff_id'] = !empty($input['staff_id'])?$input['staff_id']:$this->getUserId();

        $store_id = $this->model->getStoreId($input['staff_id']);

        $dis[] = ['class_status','=',1];

        if(!empty($name)){

            $dis[] = ['name','like','%'.$name.'%'];
        }

        $cate_id = !empty($input['cate_id'])?$input['cate_id']:0;
        //只查询含有私教课的教练
        $coach_id = $card_model->privateClassCoach($this->_uniacid,$cate_id);

        $dis[] = ['fans_id','in',$coach_id];

        $data = $card_model->coachList($store_id,$dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-22 15:02
     * @功能说明:教练列表
     */
    public function coachList(){

        $input = $this->_param;

        $card_model = new CardList();

        $store_id = $this->model->getStoreId($input['staff_id']);

        $dis[] = ['class_status','=',1];

        if(!empty($name)){

            $dis[] = ['name','like','%'.$name.'%'];
        }
        //只查询含有私教课的教练
        $coach_id = $card_model->classCoach($this->_uniacid);

        $dis[] = ['fans_id','in',$coach_id];

        $data = $card_model->coachList($store_id,$dis);

        return $this->success($data);

    }






    /**
     * @author chenniang
     * @DataTime: 2021-05-19 19:12
     * @功能说明:教练详情
     */
    public function coachInfo(){

        $input = $this->_param;

        $user_model = new IndexUserInfo();

        $data['coach_name']   = lbUserName($input['coach_id']);

        $data['coach_avatar'] = lbUserAvatar($input['coach_id']);

        $data['desc'] = $user_model->where(['fans_id'=>$input['coach_id']])->value('desc');

        $data['staff_id'] = $user_model->where(['fans_id'=>$input['coach_id']])->value('id');


        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-19 16:46
     * @功能说明:预约列表
     */
    public function userRecordList(){

        $this->date_model->cancelfailClass($this->_uniacid);

        $input = $this->_param;

        $dis = [

            'user_id' => $this->getUserId()
        ];

        if(!empty($input['status'])){

            $dis['status'] = $input['status'];
        }

        $data = $this->record_model->dataList($dis);

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-19 18:05
     * @功能说明:签到
     */
    public function sign(){

        $input = $this->_input;

        $data = $this->record_model->dataInfo(['id'=>$input['id']]);

        if(!empty($data['status'])!=1){

            $this->errorMsg('状态错误');
        }

        $res = $this->record_model->dataUpdate(['id'=>$input['id']],['status'=>3]);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-19 18:09
     * @功能说明:取消
     */
    public function cancel(){

        $input = $this->_input;

        $res   = $this->record_model->recordCancel($input['id'],0);

        return $this->success($res);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-19 18:32
     * @功能说明:获取可以使用的会员卡
     */
    public function getCanUseCard(){

        $input = $this->_param;

        $user_id = !empty($input['user_id'])?$input['user_id']:$this->getUserId();
        //时长
        $time_long = $this->model->where(['id'=>$input['class_id']])->value('time_long');

        $input['end_time'] = $input['start_time']+$time_long;

        $class_card_model = new ClassCard();

        $data = $class_card_model->canUseCard($input,$user_id);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-19 18:39
     * @功能说明:预约私交课
     */
    public function appPrivateClass(){

        $input = $this->_input;

        $res   = $this->record_model->appPrivateClass($input,$this->_uniacid,$this->getUserId());

        return $this->success($res);

    }





}
